package controllerService

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisRpc"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"time"
)

type CollectorSubscribeService struct {
}

// Scheduler 创建一个线程
func (e *CollectorSubscribeService) Scheduler() {
	go e.scheduler()
}

// 线程函数
func (e *CollectorSubscribeService) scheduler() {
	for true {
		if !commEntityManager.EntityManager.Initialized {
			time.Sleep(1 * time.Second)
			continue
		}

		// 检测：持久化服务接收队列是否堵塞了
		taskRespondVO, err := commRedisRpc.Device.Client.Report.Pop(1 * time.Second)
		if err != nil {
			time.Sleep(1 * time.Second)
			continue
		}
		if taskRespondVO == nil || taskRespondVO.RespondVOS == nil {
			continue
		}

		// 讲上报数据中的操作对象，逐个推送到redis
		for _, operateRespondVO := range taskRespondVO.RespondVOS {
			deviceEntity := commRedisService.GetDeviceEntity(operateRespondVO.DeviceName)
			if deviceEntity == nil {
				continue
			}

			reportVO := commRedisRpc.Type.TaskRespondVO.BuildRespondVO(operateRespondVO, "")
			err := commRedisRpc.Persist.Client.RecordRequest.Push(reportVO)
			if err != nil {
				continue
			}
		}
	}
}
